<?php

namespace app\admin\controller\user;

use app\common\controller\Backend;
use app\common\library\Auth;
use think\Db;
use wxApp\WxApp;

/**
 * 车商审核
 *
 * @icon fa fa-user
 */
class UserAuditing extends Backend
{

    protected $relationSearch = true;
    protected $searchFields = 'nickname,name,mobile,shop_name';

    /**
     * @var \app\admin\model\User
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\User;
    }

    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model

                ->where("shop_name","<>","")
                ->where("shop_status","<>",1)
                ->where($where)
                ->order($sort, $order)
                ->paginate($limit);
            foreach ($list as $k => $v) {
                $v->avatar = $v->avatar ? cdnurl($v->avatar, true) : letter_avatar($v->nickname);
                $v->hidden(['password', 'salt']);
            }
            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }



    //审核 通过
    public function shenhe()
    {

        $id = input("id");
        if (empty($id)){
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $user = Db::name("user")->where("id",$id)->find();
        Db::name("user")->where("id",$id)->update([
            "shop_audit_time"=>time(),
            "shop_status"=>1,
            "rejection_info"=>""
        ]);

        $data_template = [
            "phrase1" => ["value" => "审核通过"],
            "time2" => ["value" => date("Y-m-d H:i:s")]
        ];
        (new WxApp)->sendMessage("VHqSMOIdI0iCU4tJh6ve7x1h9RNwIDdwQ6NTvnxRmC0",$user["openid"],$data_template);

        $this->success("审核成功");
    }


    //审核 驳回
    public function bohui()
    {

        $id = input("id");
        $info = input("rejection_info");
        if (empty($id)&&empty($info)){
            $this->error(__('Parameter %s can not be empty', ''));
        }

        Db::name("user")->where("id",$id)->update([
            "shop_audit_time"=>time(),
            "shop_status"=>2,
            "rejection_info"=>$info
        ]);

        $user = Db::name("user")->where("id",$id)->find();
        $data_template = [
            "phrase1" => ["value" => "审核被驳回"],
            "time2" => ["value" => date("Y-m-d H:i:s")]
        ];
        (new WxApp)->sendMessage("VHqSMOIdI0iCU4tJh6ve7x1h9RNwIDdwQ6NTvnxRmC0",$user["openid"],$data_template);
        $this->success("已驳回");
    }


}
